//
//  BKAnswerScorllView.m
//  智能备考学习
//
//  Created by 盛超 on 2018/8/6.
//  Copyright © 2018年 盛超. All rights reserved.
//

#import "BKAnswerScorllView.h"
#import "BKSelectAnswerTableView.h"

@interface BKAnswerScorllView ()<UIScrollViewDelegate>

@property (nonatomic,strong) NSMutableArray *answerTableViewArr;

@end

@implementation BKAnswerScorllView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.delegate = self;
        self.bounces = YES;
        self.pagingEnabled = YES;
        self.answerTableViewArr = [NSMutableArray array];
        self.answerDict = [NSMutableDictionary dictionary];
    }
    
    return self;
}

- (void)setQuesModel:(LSModelQues *)quesModel {
    _quesModel = quesModel;
    [self addAnswerTableView];
}

//刷新界面
- (void)reloadScorllView {
    self.userInteractionEnabled = YES;
    for(NSInteger i = 0;i<self.answerTableViewArr.count;i++) {
        BKSelectAnswerTableView *tableView = self.answerTableViewArr[i];
        tableView.answerState = self.answerState;
        tableView.answer = [self.answerDict objectForKey:[NSString stringWithFormat:@"%ld",i+1]];
        [tableView reloadData];
    }
}

- (void)addAnswerTableView {

    for (NSInteger i=0; i<_quesModel.ChildList.count; i++) {
        //小题内容
        LSChild *child = _quesModel.ChildList[i];
        CGFloat tableViewHeight = self.height;//(kHeight-kNavgationBar-70)-(kHeight/3-kNavgationBar);
        BKSelectAnswerTableView *selectAnswerTableView = [[BKSelectAnswerTableView alloc] initWithFrame:CGRectMake(i*kWidth,0 , kWidth, tableViewHeight)];
        
        selectAnswerTableView.answerState = self.answerState;
        selectAnswerTableView.answer = [self.answerDict objectForKey:[NSString stringWithFormat:@"%ld",i+1]];
      
        selectAnswerTableView.tag = 2000+i;
        selectAnswerTableView.currentIndex = i+1;
        selectAnswerTableView.quesModel = _quesModel;
        selectAnswerTableView.child = child;
        [self addSubview:selectAnswerTableView];
        [self.answerTableViewArr addObject:selectAnswerTableView];
        
        WeakObj(self);
        selectAnswerTableView.answerBlock = ^(NSString *answer) {
            [selfWeak.answerDict setObject:answer forKey:[NSString stringWithFormat:@"%ld",i+1]];
            selfWeak.answerBlock(selfWeak.answerDict);
        };
    }
    
    self.contentSize = CGSizeMake(kWidth*_quesModel.ChildList.count, 0);
}


#pragma mark - ScrollViewDelegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    //滑动时更新view高度
    dispatch_async(dispatch_get_main_queue(), ^{
        
    
    });
}

//- (void)setAnswerViewHeight:(CGFloat)answerViewHeight {
//    _answerViewHeight = answerViewHeight;
//
//    for(BKSelectAnswerTableView *tabelView in _answerTableViewArr) {
//        tabelView.height = answerViewHeight;
//    }
//}

- (void)updateCurrentViewHeight {
    NSInteger currentIndex = self.contentOffset.x/kWidth;
    BKSelectAnswerTableView *tabelView = _answerTableViewArr[currentIndex];
    tabelView.height = self.height;
}

- (void)updateAllViewHeight {
    for(BKSelectAnswerTableView *tabelView in _answerTableViewArr) {
        tabelView.height = self.height;
    }
}


- (void)layoutSubviews {
//    NSInteger index = self.contentOffset.x/kWidth;
//    UIView *view = self.answerTableViewArr[index];
//    view.height = self.height;
}

@end
